# coding:utf-8
import os
import time
import requests

from bs4 import BeautifulSoup
from requests import Session, Request, exceptions

from zey.common.tools.conver import *
from zey.common.tools.data import getCurrentTimeMillis


# 网页解析器
class htmlParser(object):
    def __init__(self):
        print("")

    def soup(self, html_cont):
        soup = BeautifulSoup(html_cont, 'lxml', from_encoding='utf-8')
        return soup

    def parse(self, page_url, html_cont):
        soup = BeautifulSoup(html_cont, 'lxml', from_encoding='utf-8')


# url管理器
class urlManager(object):
    def __init__(self):
        self.new_urls = set()
        self.old_urls = set()

    # 添加url
    def add_new_url(self, url):
        if url is None:
            return
        # 当url既不在新的列表中也不在旧的列表中时，则将其添加到新的url——set()集合中
        if url not in self.new_urls and url not in self.old_urls:
            self.new_urls.add(url)

    def add_new_urls(self, urls):
        if urls is None or len(urls) == 0:
            return
        for url in urls:
            self.add_new_url(url)

    # 判断是否还有待爬取的url
    def has_new_url(self):
        return len(self.new_urls) != 0

    def get_new_url(self):
        new_url = self.new_urls.pop()
        self.old_urls.add(new_url)
        return new_url


class connection(object):
    def __init__(self):
        user_agent = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'
        self.requestType = "post"
        self.requestCoding = "utf-8"
        self.requestConnetTime = 10
        self.header = {'User_Agent': user_agent}
        self.session = Session()

    def send_get(self, url, params=None, auth=None, cookies=None, files=None):
        '''

        :param url: 
        :param params: {"key","values"} 
        :return: 
        '''
        try:
            rget = Request('GET', url, params=params, headers=self.header, files=files, auth=auth, cookies=cookies)
            # rget.encoding = "utf-8"
            prepare = rget.prepare()
            self.resp = self.session.send(prepare)
        except exceptions.ConnectTimeout as e:
            print("e : " + str(e))
        except exceptions.HTTPError as e:
            print("e : " + str(e))
        else:
            print("status_code:\t" + str(self.resp.status_code))
            # print("cookies" + self.resp.cookies)
            # print(self.resp.text)
        finally:
            return self.resp.content

    def send_post(self, url, data=None, auth=None, cookies=None, files=None):
        '''

        :param url: 
        :param data:{"key","values"} 
        :return: 
        '''
        with self.session as session:
            try:
                rpost = Request('POST', url, data=data, headers=self.header, files=files, auth=auth, cookies=cookies)
                # rpost.encoding = "utf-8"
                prepare = rpost.prepare()
                self.resp = session.send(prepare)
            except exceptions.ConnectTimeout as e:
                print("e : " + str(e))
            except exceptions.HTTPError as e:
                print("e : " + str(e))
            else:
                print("status_code:\t" + str(self.resp.status_code))
                # print("cookies" + self.resp.cookies)
                # print(self.resp.text)
            finally:
                return self.resp.content
                # def getContent(self):
                #     if(self.resp):

if __name__ == "__main__":
    # from zey.common.tools.conn import *

    URL = 'http://detail.ju.taobao.com/home.htm?spm=608.2214381.2.1.SY0wVT&item_id=16761325430&id=10000002801432'
    content = connection().send_get(URL)
    print("content \n :" + str(content))